<?php
namespace Admin\Controller;
use Think\Controller;
class ConferenceController extends Controller {
    protected $model;
    public function _initialize(){
        $this->model = D('Conference');
    }
    //查询数据，进入列表页
    public function index(){
        $data = array('aid'=>session('admin.id'));
        $count  = $this->model->where($data)->count();
        $Page   = new \Think\Page($count,6);
        $show   = $Page->show();
        $list = $this->model->where($data)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        
        foreach ($list as $k=>$v){
            $list[$k]['start_time']=date("Y-m-d H:i:s",$v['start_time']);
            $list[$k]['end_time']=date("Y-m-d H:i:s",$v['end_time']);
        }
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('Data',json_encode($list));
        $this->display("Conference/index");
    }
    //进入添加页
    public function add(){
        $this->display("Conference/add");
    }
    //根据id查询数据，进入修改页
    public function edit(){
        $id = I('get.id/d',0);
        $data = $this->model->where('id='.$id)->find();
        $this->assign('data',$data);
        $this->display("Conference/add");
    }
    //执行添加或修改操作
    public function post(){
        $data = I('post.');
        if(empty($data['id'])){
            
            if($this->model->create($data,4)){
                if($this->model->add()!==false){
                    retJson("200","添加成功！");
                }else{
                    retJson("404","添加失败！");
                }
            }else{
                retJson("404",$this->model->getError());
            }
        }else{
            if($ret = $this->model->create($data)){
                $data = array_filter($ret);//过滤值为空的键（如果没有图片上传）
                if($this->model->save($data)!==false){
                    retJson("200","修改成功！");
                }else{
                    retJson("404","修改失败！");
                }
            }else{
                retJson("404",$this->model->getError());
            }

        }
    }
    //执行删除
    public function del(){
        $id = I('get.id',0);
        if($this->model->where('id='.$id)->delete()){
            M('member')->where('cid='.$id)->delete();
            retjson("200","删除成功！");
        }else{
            retjson("404","删除失败！");
        }

    }

}